/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
var form = null;

layui.define(['form', 'layedit', 'laydate', 'zmnSelect'], function (exports) {
    let $ = layui.$;
    form = layui.form;
    let layer = layui.layer;
    let zmnSelect = layui.zmnSelect;
    let index;

    zmnLayUtil.layFormVerify(form);

    form.render(null, "zmn-form");

    zmnSelect.selectlPlatCompany('plat', 'subCompanyId', {
        plat: {
            selectedValues: json.obj.plat,
            remote: {
                limitPermit: false,
            },
            events: {
                changed: function (values) {
                    console.log("==========platId=========");
                    console.log("已选中的值:" + values);
                    $("input:radio[name='defaultType']").attr("disabled", false).prop("disabled", false);
                    $("input:radio[name='defaultType']").removeClass("layui-radio-disbaled").removeClass("layui-disabled").prop("disabled", false);
                    form.render();
                }
            }
        },
        subCompanyId: {
            selectedValues: json.obj.subCompanyId ? json.obj.subCompanyId.split(',') : [],
            remote: {
                limitPermit: false,
                events: {
                    extendParams: function () {
                        return {type: 120, status: 2}
                    }
                },
            },
            events: {
                changed: function (values) {
                    console.log("==========subCompanyId=========");
                    console.log("已选中的值:" + values);

                    let url = "/online/service/group/judgeHasDefaultServiceGroup.action";
                    let data = new Object();
                    let sysId = $("#sysId").find("option:selected").val();
                    data['groupId'] = json.obj.groupId;
                    data['sysId'] = sysId;
                    data['subCompanyIdList'] = values;
                    if (values != null && values.length > 0) {
                        $.ajax({
                            async: false,
                            type: "POST", url: url,
                            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
                            success: function (json) {
                                // 选择的对接系统+子公司已经存在默认客服组时
                                if (json.data) {
                                    $("input:radio[name='defaultType']").attr("disabled", true).prop("disabled", true);
                                    $("input:radio[name='defaultType']").addClass("layui-radio-disbaled").addClass("layui-disabled").prop("disabled", true);
                                } else {
                                    $("input:radio[name='defaultType']").attr("disabled", false).prop("disabled", false);
                                    $("input:radio[name='defaultType']").removeClass("layui-radio-disbaled").removeClass("layui-disabled").prop("disabled", false);
                                }

                                form.render();
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                            },
                            complete: function () {
                            }
                        })
                    } else {
                        $("input:radio[name='defaultType']").attr("disabled", false).prop("disabled", false);
                        $("input:radio[name='defaultType']").removeClass("layui-radio-disbaled").removeClass("layui-disabled").prop("disabled", false);
                        form.render();
                    }
                }
            }
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;

        let selectedPlat = zmnSelect.getSelectedValue("plat");
        let selectedSubCompanyId = zmnSelect.getSelectedValue("subCompanyId");
        if (selectedPlat == null || selectedPlat.length < 1) {
            layer.msg("平台不能为空！", {icon: 2});
            return false;
        }
        if (selectedSubCompanyId == null || selectedSubCompanyId.length < 1) {
            layer.msg("子公司不能为空！", {icon: 2});
            return false;
        }
        data.plat = selectedPlat[0];
        data.subCompanyId = selectedSubCompanyId.join(',');

        let url = "/online/service/group/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                layer.close(index);
                if (json.status == 200) {
                    layer.msg(json.message);
                } else {
                    layer.msg(json.message, {icon: 2});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete: function () {
                layer.close(index);
            }
        })

        return false;
    });

    //对外暴露的接口
    exports('online_service_group_detail_edit_js', {});
});
